package com.me.study.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.me.study.R
import com.me.study.app.adapater.EntranceAdapter
import com.me.study.app.base.BaseActivity
import com.me.study.app.data.EntranceType
import com.me.study.app.ext.goActivity
import com.me.study.databinding.ActivityMainBinding
import com.me.study.ui.activity.blue.SearchBluetoothActivity
import com.me.study.viewmodel.SampleViewModel
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration

class MainActivity : BaseActivity<SampleViewModel, ActivityMainBinding>(), OnItemClickListener {

    private val entranceAdapter by lazy {
        EntranceAdapter(arrayListOf())
    }

    override fun layoutId() = R.layout.activity_main

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.rvEntrance.itemAnimator = DefaultItemAnimator()
        mDatabind.rvEntrance.layoutManager = LinearLayoutManager(this)
        mDatabind.rvEntrance.adapter = entranceAdapter
        val color = Color.TRANSPARENT
        val w = SizeUtils.dp2px(10f)
        val h = SizeUtils.dp2px(10f)
        mDatabind.rvEntrance.addItemDecoration(DefaultItemDecoration(color, w, h))
        entranceAdapter.setOnItemClickListener(this)
        mViewModel.getEntranceData()
    }

    override fun createObserver() {
        mViewModel.entranceDataResult.observe(this, Observer {
            entranceAdapter.addData(it)
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val bean = entranceAdapter.data[position]
        when (bean.type) {
            EntranceType.CAMERA_X.VALUE -> {
                goActivity(CameraXActivity::class.java)
            }
            EntranceType.RvRichEditor.VALUE -> {
                goActivity(RvRichEditorActivity::class.java)
            }
            EntranceType.BLUETOOTH.VALUE -> {
                goActivity(SearchBluetoothActivity::class.java)
            }
            EntranceType.RV_HORIZONTAL_MORE.VALUE -> {
                goActivity(HorizontalSlideActivity::class.java)
            }
            EntranceType.EMOJI.VALUE -> {
                goActivity(EmojiActivity::class.java)
            }
        }
    }
}